package com.tyjt.util;

import com.google.protobuf.ByteString;
import com.tyjt.proto.PubDataDTO;
import com.tyjt.proto.veh.Veh2CloudBsmDTO;

public class GenProtoUtil {
    public static String toStr(byte[] bytes) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            // 使用位运算将byte转换为无符号int，并格式化为两位16进制字符串
            String hex = String.format("%02x", b & 0xFF);
            hexString.append(hex);
        }
        String s = hexString.toString();
        System.out.println(s);
        return s;
    }

    public static void main(String[] args) {
        genPub();
        genBsm();
    }

    private static void genBsm() {
        Veh2CloudBsmDTO.Veh2CloudBsm.Builder builder = Veh2CloudBsmDTO.Veh2CloudBsm.newBuilder();
        builder.setVehicleId("chongxin");
        builder.setLongitude(116.397128);
        builder.setLatitude(39.90923);
        System.out.println("bsm:");
        toStr(builder.build().toByteArray());
    }

    private static void genPub() {
        PubDataDTO.PubData.Builder pubData = PubDataDTO.PubData.newBuilder();
        PubDataDTO.MetaData.Builder meta = PubDataDTO.MetaData.newBuilder();
        meta.setCompanyId("1");
        meta.setMsgType("1");
        meta.setUid("123");
        meta.setEffectRadiusCm(100*100);

        meta.setLongitude(116.397128);
        meta.setLatitude(39.90923);
        pubData.setMeta(meta);
        pubData.setPayload(ByteString.copyFrom("hello world".getBytes()));
        System.out.println("pub:");

        toStr(pubData.build().toByteArray());
//        EmqxUtil.sendMsg("idse/cloud/1/1",pubData.build().toByteArray());

//        pub:idse/cloud/1/1
//0a220a0131120331323322013129db148f8b6a195d403163450da661f443404d00401c46120b68656c6c6f20776f726c64
//bsm:idse/bsm/1/1
//0a0863686f6e6778696e19db148f8b6a195d402163450da661f44340
    }
}
